package database;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

/**
 * Responsible for write things down to the database.
 * 
 * @author Cassio Soares Cabral
 * 
 */
public class Writer extends Update {

	/**
	 * Write something in the DataBase by the SQL command passed as argument.
	 * 
	 * @param SQLCommand
	 */
	private void writeInDB(String SQLCommand)
			throws MySQLIntegrityConstraintViolationException {
		super.update(SQLCommand);
	}

	/**
	 * Write a sponsor in the database.
	 * 
	 * @param name
	 * @param monthly
	 * @param type
	 */
	public void writeSponsor(String name, Double monthly, String type) {
		try {
			writeInDB("INSERT INTO sponsor (name, monthly, type)" + "VALUES ('"
					+ name + "', '" + monthly + "', '" + type + "')");
		} catch (MySQLIntegrityConstraintViolationException e) {
			System.err.println("Error to write, probabily entry was repeated.");
		}

	}

	/**
	 * Write the name of sponsor and a video path in the table namePath that
	 * relates sponsor with their videos.
	 * 
	 * @param nameSponsor
	 * @param pathVideo
	 */
	public void writeNamePath(String nameSponsor, String pathVideo) {
		try {
			writeInDB("INSERT INTO namepath (name, path)" + "VALUES ('"
					+ nameSponsor + "', '" + pathVideo + "')");
		} catch (MySQLIntegrityConstraintViolationException e) {
			System.err.println("Error to write, probabily entry was repeated.");
		}

	}

	/**
	 * Write the name of sponsor and a video path in the table namePath that
	 * relates sponsor with their videos.
	 * 
	 * @param nameSponsor
	 * @param pathVideo
	 * @param size
	 * @param lenght
	 */
	public void writeVideo(String nameSponsor, String pathVideo, Integer size,
			Integer lenght) {
		try {
			writeInDB("INSERT INTO video (path, name, size, length)"
					+ "VALUES ('" + pathVideo + "', '" + nameSponsor + "', '"
					+ size + "', '" + lenght + "')");
		} catch (MySQLIntegrityConstraintViolationException e) {
			System.err.println("Error to write, probabily entry was repeated.");
		}

	}

	/**
	 * Write rss informations in the table rss.
	 * 
	 * @param url
	 * @param feed
	 * @param lastUpdate
	 */
	public void writeRss(String url, String feed, String lastUpdate) {
		try {
			writeInDB("INSERT INTO rss (url, feed, lastUpdate)" + "VALUES ('"
					+ url + "', '" + feed + "', '" + lastUpdate + "')");
		} catch (MySQLIntegrityConstraintViolationException e) {
			System.err.println("Error to write, probabily entry was repeated.");
		}

	}

	/**
	 * Write news information(headline of the news and the feed that it belongs)
	 * in the table news.
	 * 
	 * @param title
	 * @param feed
	 */
	public void writeNews(String title, String feed) {
		try {
			writeInDB("INSERT INTO news (title, feed)" + "VALUES ('" + title
					+ "', '" + feed + "')");
		} catch (MySQLIntegrityConstraintViolationException e) {
			System.err.println("Error to write, probabily entry was repeated.");
		}

	}

}
